﻿using Authentication.Model.Interface;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Core.ServiceCommand
{
    public class RetrieveCloseDaoInfoByIdServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveCloseDaoInfoRequest, RetrieveCloseDaoInfoResponse>
    {

        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public string TextSearch;
        private UserRecord currentUser;
        public RetrieveCloseDaoInfoByIdServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveCloseDaoInfoRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("User record must be set", "request");
            }
            else
            {
                currentUser = request.CurrentUser;
            }
            if (Request.CurrentUser == null)
            {
                throw new ArgumentException("Dao must be set", "request");
            }
        }

        protected override bool OnExecute(RetrieveCloseDaoInfoResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            var applicationKey = Request.Header.ApplicationKey;
            try
            {
                var request = Providers.CloseDao.RetrieveRequestByRequestId(Request.RequestId, currentUser);
                var DaoInfo = Providers.Users.RetrieveUserByDao(request.Dao, currentUser);
                var daoCode16 = Providers.SaleCode.RetrieveDaoCodeByDao(request.Dao, currentUser);
                CloseDaoRequestRecord record = new CloseDaoRequestRecord
                {
                    FullName = daoCode16.DaoName,
                    Email = DaoInfo == null ? string.Empty : DaoInfo.Email,
                    Dao = request.Dao,
                    //Phone = DaoInfo.Phone,
                    Cif = daoCode16.Cif,
                    Branch = daoCode16.Division,
                    Pic = request.Pic,
                    PositionName = daoCode16.DeliveryPoint,
                    Status = request.Status,
                    //LegalId = daoCode16.LegalId,
                    StaffId = daoCode16.StaffId
                };
                response.Record = record;
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                response.ErrorMessage = string.Format("Can not found user with Dao={0}", Request.CurrentUser.Dao);
                response.IsSuccess = false;
            }
            return isSuccess;
        }
    }
}
